package com.yiyuan.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.yiyuan.common.base.BaseDao;
import com.yiyuan.system.dao.IUserDao;
import com.yiyuan.system.model.User;
import com.yiyuan.system.object.UserObject;

@Repository("userDao")
public class UserDaoHbImpl extends BaseDao implements IUserDao {
	public void add(User user) {
		this.getHibernateTemplate().save(user);
	}

	@SuppressWarnings("unchecked")
	public List<User> findList(int startIndex, int endIndex, Integer deptId,
			Integer empId) {
		Session session = this.getSession();
		StringBuffer sbsql = new StringBuffer(100);
		boolean empFlag = false;
		boolean deptFlag = false;
		if (empId != null) {
			empFlag = true;
		}
		if (deptId != null) {
			deptFlag = true;
		}
		sbsql.append("select user.user_id,user.user_name,emp.emp_id,emp.emp_name  ");
		sbsql.append("from yy_sys_user user,yy_sys_employee emp where user.emp_id=emp.emp_id ");
		if (empFlag) {
			sbsql.append(" and emp.emp_id=:empId");
		}
		if (deptFlag) {
			sbsql.append(" and emp.dept_id=:deptId");
		}

		Query query = session.createSQLQuery(sbsql.toString());
		if (empFlag) {
			query.setInteger("empId", empId);
		}
		if (deptFlag) {
			query.setInteger("deptId", deptId);
		}
		query.setFirstResult(startIndex - 1);
		query.setMaxResults(endIndex);

		List<User> userList = new ArrayList<User>();
		User user = null;
		List list = query.list();
		for (int i = 0; i < list.size(); i++) {
			Object users[] = (Object[]) list.get(i);
			user = new User();

			user.setUserId(Integer.valueOf(users[0].toString()));
			user.setUserName(users[1].toString());
			user.setEmpId(Integer.valueOf(users[2].toString()));
			user.setEmpName(users[3].toString());

			userList.add(user);
		}

		return userList;
	}

	public int getPageCount(Integer deptId, Integer empId) {
		Session session = this.getSession();
		StringBuffer sbsql = new StringBuffer(100);
		boolean empFlag = false;
		boolean deptFlag = false;
		if (empId != null) {
			empFlag = true;
		}
		if (deptId != null) {
			deptFlag = true;
		}
		sbsql.append("select count(user.user_id) ");
		sbsql.append("from yy_sys_user user,yy_sys_employee emp where user.emp_id=emp.emp_id ");
		if (empFlag) {
			sbsql.append(" and emp.emp_id=:empId");
		}
		if (deptFlag) {
			sbsql.append(" and emp.dept_id=:deptId");
		}

		Query query = session.createSQLQuery(sbsql.toString());
		if (empFlag) {
			query.setInteger("empId", empId);
		}
		if (deptFlag) {
			query.setInteger("deptId", deptId);
		}
		int count = Integer.parseInt(query.uniqueResult().toString());
		return count;
	}

	public void update(User user) {
		this.getHibernateTemplate().update(user);
	}

	public User get(Integer userId) {
		return (User) this.getHibernateTemplate().get(User.class, userId);
	}

	public void delete(User user) {
		this.getHibernateTemplate().delete(user);
	}

	public User getUserByNameAndPwd(String userName, String password) {
		Session session = this.getSession();
		Query query = session
				.createQuery("from User where user_name=:userName and password=:password ");
		query.setString("userName", userName);
		query.setString("password", password);
		User user = (User) query.uniqueResult();
		return user;
	}

	@SuppressWarnings("unchecked")
	public List<UserObject> getUserList(String deptId) {
		List userList = new ArrayList();
		UserObject uo = null;
		Session session = this.getSession();
		Query query = session
				.createSQLQuery("select u.user_id,u.user_name,e.emp_id,e.emp_name,e.dept_id "
						+ " from yy_sys_user u,yy_sys_employee e where u.emp_id=e.emp_id and e.dept_id="
						+ deptId);
		List list = query.list();

		for (int i = 0; i < list.size(); i++) {
			Object users[] = (Object[]) list.get(i);
			uo = new UserObject();
			uo.setUserId(users[0].toString());
			uo.setUserName(users[1].toString());
			uo.setEmpId(users[2].toString());
			uo.setEmpName(users[3].toString());
			uo.setDeptId(users[4].toString());
			userList.add(uo);
		}
		return userList;
	}
}
